package com.yanqu.road.server.cmd.cricket;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cricketcourt.CrossCricketCourtMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_CRICKET_MATCH, desc = "竞技场匹配")
public class CrossCricketMatchCmd extends CrossCommand {
    private boolean isRefresh;

    long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CricketProto.CrossCricketMatchReqMsg reqMsg = CricketProto.CrossCricketMatchReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        isRefresh = reqMsg.getIsRefresh();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ret = CrossCricketCourtMgr.match(serverId, userId, isRefresh);
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            // 匹配失败就回游戏服处理
            CricketProto.CrossCricketMatchRespMsg.Builder crossResp = CricketProto.CrossCricketMatchRespMsg.newBuilder();
            crossResp.setRet(ret);
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(Protocol.S_CRICKET_MATCH_FROM_CROSS, crossResp));
        }
    }
}
